package com.study.it.service.impl;

import com.study.it.dao.BaseDao;
import com.study.it.entity.PageInfo;
import com.study.it.service.BaseService;
import org.hibernate.Criteria;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Projections;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.List;

public class BaseServiceImpl<T,D extends BaseDao<T>> implements BaseService<T>  {
	protected D dao = null;
	@Autowired
	public void setDao(D dao) {
		this.dao = dao;
	}
	@Override
	public void add(T o) {
		dao.insert(o);
	}
	@Override
	public void edit(T o) {
	    dao.update(o);
	}
	@Override
	//@Transactional
	public void remove(Serializable id) {
		dao.delete(id);
	}
	@Override
	//@Transactional
	public void remove(T o) {
		dao.delete(o);
	}
	@Override
	//@Transactional(readOnly = true)
	public T loadOne(Serializable id) {
	     return dao.selectOne(id);
	}
	@Override
	//@Transactional(readOnly = true)
	public T findOne(Serializable id) {
		return dao.selectOne(id);
	}
	@Override
	//@Transactional(readOnly = true)
	public T findOne(DetachedCriteria dc) {
		return dao.selectOne(dc);
	}
	@Override
	//@Transactional(readOnly = true)
	public Object findSingle(DetachedCriteria dc) {
		return dao.selectSingle(dc);
	}
	@Override
	//@Transactional(readOnly = true)
	public List<T> findList() {
		 return dao.selectList();
	}
	@Override
	//@Transactional(readOnly = true)
	public List<T> findList(DetachedCriteria dc) {
		return dao.selectList(dc);
	}
	@Override
	//@Transactional(readOnly = true)
	public List<T> findList(DetachedCriteria dc, boolean iscache) {
		return dao.selectList(dc,iscache);
	}
	@Override
	//@Transactional(readOnly = true)
	public List<T> findList(DetachedCriteria dc, int pageIndex, int row) {
		//利用传入的条件，返回集合和行数
		return this.findList(dc,pageIndex,row,false);
	}
	@Override
	//@Transactional(readOnly = true)
	public List<T> findList(DetachedCriteria dc, int pageIndex, int row, boolean isCache) {
		return dao.selectList(dc,(pageIndex-1)*row,row,isCache);
	}
	@Override
	//@Transactional(readOnly = true)
	public int findDataCount(DetachedCriteria dc) {
		Long l= (Long) dao.selectSingle(dc);
		return l.intValue();
	}
	@Override
	//@Transactional(readOnly = true)
	public void refresh(T o) {
		dao.refresh(o);
	}
	@Override
	//@Transactional(readOnly = true)
	public PageInfo<T> findPage(DetachedCriteria dc, int pageIndex, int row) {
		return this.findPage(dc, pageIndex, row,false);
	}
	@Override
	//@Transactional(readOnly = true)
	public PageInfo<T> findPage(DetachedCriteria dc, int pageIndex, int row, boolean isCache) {
		PageInfo<T> pageInfo=new PageInfo<T>();
		//要先找总页数(涉及到dc的重用问题,先找列表的话,分页行数的设定清不掉)
		dc.setProjection(Projections.rowCount());//压入数据总行数
		pageInfo.setDataCount(this.findDataCount(dc));
		
		dc.setProjection(null);
		dc.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
		pageInfo.setList(this.findList(dc,pageIndex,row,isCache));//压入集合
		
		pageInfo.setRow(row);//压入每页多少行
		pageInfo.setCurrentPage(pageIndex);//压入当前页
		return pageInfo;
	}
}




